package com.chuangyang.fixboxmaster.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chuangyang.fixboxmaster.AppData;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.ui.PhotoViewActivity;

import java.util.ArrayList;

/**
 * Created by vanshu on 15/4/14 11:04.
 */
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {

    private ArrayList<String> imageUrls;
    private Context mContext;

    public GalleryAdapter(Context context,ArrayList<String> imageUrls) {
        this.mContext=context;
        this.imageUrls = imageUrls;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(AppData.getContext()).inflate(R.layout.gallery_list_item, parent, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(onClickListener);
        return vh;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(AppData.getContext(), PhotoViewActivity.class);
            intent.putExtra("photo_url", imageUrls);
            intent.putExtra("currentPosition",(int)v.getTag());
            mContext.startActivity(intent);
        }
    };


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(AppData.getContext()).load(imageUrls.get(position)).placeholder(R.drawable.ic_order_detail_empty_view).centerCrop().crossFade().into(holder.mImage);
        holder.mImage.setTag(position);
    }

    @Override
    public int getItemCount() {
        return imageUrls == null ? 0 : imageUrls.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImage;

        public ViewHolder(View v) {
            super(v);
            mImage = (ImageView) v.findViewById(R.id.image);
        }
    }
}
